<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/OutsourceNotice.jsx或OutsourceNotice.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
        :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
        :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
        :searchAfter="searchAfter" :addBefore="addBefore" :updateBefore="updateBefore" :rowClick="rowClick"
        :modelOpenBefore="modelOpenBefore" :modelOpenAfter="modelOpenAfter">
        <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
        <template #gridHeader>
        </template>
        <template #gridFooter>
            <div class="outsourceProcessReport">
                <outsourceProcessReport ref="outsourceProcessReportRef"></outsourceProcessReport>
            </div>
        </template>
    </view-grid>
</template>
<script setup lang="jsx">
import outsourceProcessReport from './OutsourceProcessReport.vue'
import extend from "@/extension/mes/mes/OutsourceNotice.jsx";
import viewOptions from './OutsourceNotice/options.js'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
import { foreach } from '@visactor/vtable/es/vrender';
const grid = ref(null);
const { proxy } = getCurrentInstance()
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
let selectRows;
const outsourceProcessReportRef = ref();
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    gridRef.buttons.splice(3, 0, {
        name: '委外报工', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'primary',
        plain: true,
        onClick: () => {
            outsourceProcessReportAddOpen()
        }
    })
    proxy.base.setItem('getNoticeSelectRow', () => {
        return gridRef.getTable(true).getSelected();
    })

    proxy.base.setItem('noticeSearch', (row) => {
        selectRows = row
        gridRef.search()
    })

    gridRef.detailOptions.sortable = true

    gridRef.detailOptions.pagination.sortName = "OutsourceProcessReportId";  //明细表排序字字段
    gridRef.detailOptions.pagination.order = "asc"; //明细表排序方式desc或者asc
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })

    gridRef.height = gridRef.height - 310;
    if (gridRef.height < 200) {
        gridRef.height = 200;
    }
}
const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    return true;
}
const searchAfter = async (rows, result) => {
    if (selectRows != null) {
        rows.forEach((row) => {
            if (selectRows.OutsourceNoticeId == row.OutsourceNoticeId){
                grid.value.toggleRowSelection(row);
            }
        });
    }
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const rowClick = ({ row, column, event }) => {
    grid.value.clearSelection();
    //点击行默认选中
    grid.value.toggleRowSelection(row); //单击行时选中当前行;
    outsourceProcessReportRef.value.$refs.grid.search();
}
const outsourceProcessReportAddOpen = () => {
    let selectRows = gridRef.getSelectRows();
    if (selectRows.length == 0) {
        proxy.$message.error("请选择委外通知！");
        return
    }
    if (selectRows[0].AuditStatus == 0 || selectRows[0].AuditStatus == null) {
        proxy.$message.error("该委外通知未审核！");
        return
    }
    if (selectRows[0].CompleteStatusId == 1) {
        proxy.$message.error("该委外通知【已完工】，不允许报工！");
        return
    }

    setTimeout(() => {

        outsourceProcessReportRef.value.$refs.grid.editFormFields.OutsourceNoticeId = selectRows[0].OutsourceNoticeId
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ProductTaskProcessId = selectRows[0].ProductTaskProcessId
        outsourceProcessReportRef.value.$refs.grid.editFormFields.BillDate = proxy.base.getDate()
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ProductProcessNo = selectRows[0].ProductProcessNo
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ProductProcessId = selectRows[0].ProductProcessId
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ProductProcessName = selectRows[0].ProductProcessName
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ProductProcessTypeId = selectRows[0].ProductProcessTypeId
        outsourceProcessReportRef.value.$refs.grid.editFormFields.DepartmentId = selectRows[0].DepartmentId
        outsourceProcessReportRef.value.$refs.grid.editFormFields.DepartmentName = selectRows[0].DepartmentName
        outsourceProcessReportRef.value.$refs.grid.editFormFields.GroupId = selectRows[0].GroupId
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ReportWorkTypeId = selectRows[0].ReportWorkTypeId

        //待派工数量
        outsourceProcessReportRef.value.$refs.grid.editFormFields.WaitReportQty = selectRows[0].PlanQty - selectRows[0].QualifyQty
        //每次报工数量=派工数量-合格数量
        outsourceProcessReportRef.value.$refs.grid.editFormFields.QualifyQty = selectRows[0].PlanQty - selectRows[0].QualifyQty
        outsourceProcessReportRef.value.$refs.grid.editFormFields.UnQualifyQty = 0
        outsourceProcessReportRef.value.$refs.grid.editFormFields.ScrapQty = 0


    }, 400);
    outsourceProcessReportRef.value.$refs.grid.add()
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {
    //弹出框打开后方法,设置表单默认值,按钮操作等
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
<style lang="less" scoped>
.outsourceProcessReport {
    margin-top: 13px;
    border-top: 7px solid #eee;
}
</style>
